package spring.demo.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
import org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator;
import org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator;
import org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor;

import java.util.Arrays;

/**
 * Aspect 这个注解告诉spring 这是一个切面类  配合@Bean 注入到sring种
 *
 * -> AspectJAutoProxyRegistrar
 *        @see AnnotationAwareAspectJAutoProxyCreator
 *          @see AspectJAwareAdvisorAutoProxyCreator
 *              @see AbstractAdvisorAutoProxyCreator
 *                  @see AbstractAutoProxyCreator
 *                      @see SmartInstantiationAwareBeanPostProcessor
 *                      @see BeanFactoryAware
 */
@Aspect
public class AopConfig {


    @Pointcut("execution(public int spring.demo.aop.CustomerPlus.*(..))")
    public void pointCut() {

    }

    @Before("pointCut()")
    public void start(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        String className = joinPoint.getSignature().getDeclaringType().getSimpleName();
        String methodName = joinPoint.getSignature().getName();
        System.out.println(className + "." + methodName + "()");
        System.out.println("方法 ： " + joinPoint.getSignature().getName() + "计算入参" + Arrays.asList(args));
    }


    @After("pointCut()")
    public void end(JoinPoint joinPoint) {

        System.out.println("方法 ： " + joinPoint.getSignature().getName() + "方法返回 end ");
    }

    @AfterThrowing(value = "pointCut()",throwing = "exception")
    public void throwException(JoinPoint joinPoint,Exception exception) {

        System.out.println("方法 ： " + joinPoint.getSignature().getName() + "异常返回 throwException " + exception.getMessage());
    }

    /**
     * 成功返回
     *
     * @param joinPoint
     */
    @AfterReturning(value = "pointCut()", returning = "result")
    public void AfterReturning(JoinPoint joinPoint, Object result) {

        System.out.println("方法 ： " + joinPoint.getSignature().getName() + "成功常返回 AfterReturning 返回值 " + result);
    }
}
